package com.woniu.houtai.delay;

import com.rabbitmq.client.Channel;
import com.woniu.houtai.entity.Order;
import com.woniu.houtai.mapper.OrderMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

//消费者
@Component
public class HoutaiDelayedCancelOrder {
    //结合死信队列实现延迟1分钟自动接受未拒绝的订单
    @Autowired
    OrderMapper orderMapper;

    @RabbitListener(queues = "deadLetterQueue")//死信队列

    public void abd(Integer orderId, Channel channel, Message message) throws IOException {
        //根据订单id查询订单信息,
        Order order = orderMapper.selectById(orderId);
        // 如果为已支付状态（1）,
        if (order.getStatus() == 1 ){
            // 那么就将订单状态改为已接单（4）
            order.setStatus(4);
            //更新修改状态后的订单
            orderMapper.updateById(order);
        }

        //更改ack状态，取消自动确认
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }



}
